package gdr.window.mainPage;

import gdr.dev.SuperGDR9000;
import gdr.window.menu.Menu;
import gdr.window.menu.Opzione;
import gdr.window.online.server.*;
import gdr.window.online.client.*;

public class OnlineMainPage extends Menu{
	protected OnlineServer server;
	protected OnlineClient client;
	
	public OnlineMainPage(SuperGDR9000 gdr){
		super(gdr, "Online main page");
		super.setVisible(true);
		super.setFocused(true);
		super.setNeedRefresh(true);
		
		super.setDefaultIsFocused(true);
		super.setDefaultIsVisible(true);
		super.setDefaultNeedRefresh(true);
		
		super.setAdattaAltezza(true);
		super.setAdattaLarghezza(true);
		super.setWidth(0);
		super.setHeight(0);
		super.setX(gdr.getWidth()/2);
		super.setY(gdr.getHeight()/2);
		super.setHorizontal_align(CENTER);
		super.setVertical_align(CENTER);
		
		super.addMenuOption(new Opzione("Server mode", true, true){
			public void enter(){ startServer();}
		}, 0, 0);

		super.addMenuOption(new Opzione("Client mode", true, true){
			public void enter(){ startClient();}
		}, 1, 0);
	}	
	
	protected void startServer(){
		if (server==null){
			server = new OnlineServer(gdr);
			super.addChild(server);
			server.startServer(); //avvia il server
		}
		super.hide();
	}
	
	protected void startClient(){
		if (client==null){
			client = new OnlineClient(gdr);
			super.addChild(client);
			client.startClient(); //avvia il client
		}
		super.hide();
		client.activate();
	}
}
